package com.windaka.elements.config;


import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

import javax.servlet.http.HttpServletResponse;

/**
 * 资源限制
 * @author song
 * @date 16:50 2022/8/17
**/
@EnableResourceServer
@EnableWebSecurity
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    /**
     * 路由拦截
     * @author song
     * @date 13:36 2021/11/17
     **/
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .httpBasic()
                .and()
                .headers()
                .frameOptions()
                .sameOrigin()
                .and()
                .cors();
        http
                .exceptionHandling()
                .authenticationEntryPoint(
                        (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                .and()
                // 放行的url 这里把login放行
                .authorizeRequests()
                // 放行登录
                // 放行websocket
                .antMatchers( "/Oauth/**", "/dataServer/**")
                .permitAll()
                // 其他的请求 需要进行认证
                .anyRequest()
                .authenticated();
    }

}
